﻿using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using HslCommunication.Core;
using HslCommunication.ModBus;
using HslCommunication.Reflection;

namespace HslCommunication.Profinet.Delta
{
	/// <summary>
	/// 台达PLC的网口通讯类，基于Modbus-Rtu协议开发，按照台达的地址进行实现。<br />
	/// The tcp communication class of Delta PLC is developed based on the Modbus-Tcp protocol and implemented according to Delta's address.
	/// </summary>
	/// <remarks>
	/// 适用于DVP-ES/EX/EC/SS型号，DVP-SA/SC/SX/EH型号，地址参考API文档，同时地址可以携带站号信息，举例：[s=2;D100],[s=3;M100]，可以动态修改当前报文的站号信息。<br />
	/// Suitable for DVP-ES/EX/EC/SS models, DVP-SA/SC/SX/EH models, the address refers to the API document, and the address can carry station number information,
	/// for example: [s=2;D100],[s= 3;M100], you can dynamically modify the station number information of the current message.
	/// </remarks>
	/// <example>
	/// 地址的格式如下：
	/// <list type="table">
	///   <listheader>
	///     <term>地址名称</term>
	///     <term>地址代号</term>
	///     <term>示例</term>
	///     <term>地址进制</term>
	///     <term>字操作</term>
	///     <term>位操作</term>
	///     <term>备注</term>
	///   </listheader>
	///   <item>
	///     <term></term>
	///     <term>S</term>
	///     <term>S0-S1023</term>
	///     <term>10</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>输入继电器</term>
	///     <term>X</term>
	///     <term>X0-X377</term>
	///     <term>8</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term>只读</term>
	///   </item>
	///   <item>
	///     <term>输出继电器</term>
	///     <term>Y</term>
	///     <term>Y0-Y377</term>
	///     <term>8</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>定时器</term>
	///     <term>T</term>
	///     <term>T0-T255</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>如果是读位，就是通断继电器，如果是读字，就是当前值</term>
	///   </item>
	///   <item>
	///     <term>计数器</term>
	///     <term>C</term>
	///     <term>C0-C255</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>√</term>
	///     <term>如果是读位，就是通断继电器，如果是读字，就是当前值</term>
	///   </item>
	///   <item>
	///     <term>内部继电器</term>
	///     <term>M</term>
	///     <term>M0-M4095</term>
	///     <term>10</term>
	///     <term>×</term>
	///     <term>√</term>
	///     <term></term>
	///   </item>
	///   <item>
	///     <term>数据寄存器</term>
	///     <term>D</term>
	///     <term>D0-D9999</term>
	///     <term>10</term>
	///     <term>√</term>
	///     <term>×</term>
	///     <term></term>
	///   </item>
	/// </list>
	/// </example>
	// Token: 0x020000B0 RID: 176
	public class DeltaDvpTcpNet : ModbusTcpNet
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.#ctor" />
		// Token: 0x06000CC7 RID: 3271 RVA: 0x0004C262 File Offset: 0x0004A462
		public DeltaDvpTcpNet()
		{
			base.ByteTransform.DataFormat = DataFormat.CDAB;
		}

		/// <inheritdoc cref="M:HslCommunication.ModBus.ModbusTcpNet.#ctor(System.String,System.Int32,System.Byte)" />
		// Token: 0x06000CC8 RID: 3272 RVA: 0x0004C279 File Offset: 0x0004A479
		public DeltaDvpTcpNet(string ipAddress, int port = 502, byte station = 1) : base(ipAddress, port, station)
		{
			base.ByteTransform.DataFormat = DataFormat.CDAB;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Read(System.String,System.UInt16)" />
		// Token: 0x06000CC9 RID: 3273 RVA: 0x0004C294 File Offset: 0x0004A494
		[HslMqttApi("ReadByteArray", "Read the original byte data content from the register, the address is mainly D, T, C")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 3);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = base.Read(operateResult.Content, length);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Write(System.String,System.Byte[])" />
		// Token: 0x06000CCA RID: 3274 RVA: 0x0004C2D4 File Offset: 0x0004A4D4
		[HslMqttApi("WriteByteArray", "Write the original byte data content to the register, the address is mainly D, T, C")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 16);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Read(System.String,System.UInt16)" />
		// Token: 0x06000CCB RID: 3275 RVA: 0x0004C314 File Offset: 0x0004A514
		[DebuggerStepThrough]
		public override Task<OperateResult<byte[]>> ReadAsync(string address, ushort length)
		{
			DeltaDvpTcpNet.<ReadAsync>d__4 <ReadAsync>d__ = new DeltaDvpTcpNet.<ReadAsync>d__4();
			<ReadAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<byte[]>>.Create();
			<ReadAsync>d__.<>4__this = this;
			<ReadAsync>d__.address = address;
			<ReadAsync>d__.length = length;
			<ReadAsync>d__.<>1__state = -1;
			<ReadAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<ReadAsync>d__4>(ref <ReadAsync>d__);
			return <ReadAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Write(System.String,System.Byte[])" />
		// Token: 0x06000CCC RID: 3276 RVA: 0x0004C368 File Offset: 0x0004A568
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, byte[] value)
		{
			DeltaDvpTcpNet.<WriteAsync>d__5 <WriteAsync>d__ = new DeltaDvpTcpNet.<WriteAsync>d__5();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<WriteAsync>d__5>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000CCD RID: 3277 RVA: 0x0004C3BC File Offset: 0x0004A5BC
		[HslMqttApi("ReadBoolArray", "Read the contents of bool data in batches from the coil, the address is mainly X, Y, S, M, T, C")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 1);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.ReadBool(operateResult.Content, length);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpTcpNet.Write(System.String,System.Boolean)" />
		// Token: 0x06000CCE RID: 3278 RVA: 0x0004C3FC File Offset: 0x0004A5FC
		[HslMqttApi("WriteBoolArray", "Read the contents of bool data in batches from the coil, the address is mainly X, Y, S, M, T, C")]
		public override OperateResult Write(string address, bool[] values)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 15);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, values);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Write(System.String,System.Boolean)" />
		// Token: 0x06000CCF RID: 3279 RVA: 0x0004C43C File Offset: 0x0004A63C
		[HslMqttApi("WriteBool", "Write bool data content to the coil, the address is mainly Y, S, M, T, C")]
		public override OperateResult Write(string address, bool value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 5);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000CD0 RID: 3280 RVA: 0x0004C47C File Offset: 0x0004A67C
		[DebuggerStepThrough]
		public override Task<OperateResult<bool[]>> ReadBoolAsync(string address, ushort length)
		{
			DeltaDvpTcpNet.<ReadBoolAsync>d__9 <ReadBoolAsync>d__ = new DeltaDvpTcpNet.<ReadBoolAsync>d__9();
			<ReadBoolAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult<bool[]>>.Create();
			<ReadBoolAsync>d__.<>4__this = this;
			<ReadBoolAsync>d__.address = address;
			<ReadBoolAsync>d__.length = length;
			<ReadBoolAsync>d__.<>1__state = -1;
			<ReadBoolAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<ReadBoolAsync>d__9>(ref <ReadBoolAsync>d__);
			return <ReadBoolAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpTcpNet.Write(System.String,System.Boolean)" />
		// Token: 0x06000CD1 RID: 3281 RVA: 0x0004C4D0 File Offset: 0x0004A6D0
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool[] values)
		{
			DeltaDvpTcpNet.<WriteAsync>d__10 <WriteAsync>d__ = new DeltaDvpTcpNet.<WriteAsync>d__10();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.values = values;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<WriteAsync>d__10>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.Delta.DeltaDvpSerial.Write(System.String,System.Boolean)" />
		// Token: 0x06000CD2 RID: 3282 RVA: 0x0004C524 File Offset: 0x0004A724
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, bool value)
		{
			DeltaDvpTcpNet.<WriteAsync>d__11 <WriteAsync>d__ = new DeltaDvpTcpNet.<WriteAsync>d__11();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<WriteAsync>d__11>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Int16)" />
		// Token: 0x06000CD3 RID: 3283 RVA: 0x0004C578 File Offset: 0x0004A778
		[HslMqttApi("WriteInt16", "Write short data, returns whether success")]
		public override OperateResult Write(string address, short value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 6);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.UInt16)" />
		// Token: 0x06000CD4 RID: 3284 RVA: 0x0004C5B8 File Offset: 0x0004A7B8
		[HslMqttApi("WriteUInt16", "Write ushort data, return whether the write was successful")]
		public override OperateResult Write(string address, ushort value)
		{
			OperateResult<string> operateResult = DeltaHelper.PraseDeltaDvpAddress(address, 6);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				result = base.Write(operateResult.Content, value);
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.Int16)" />
		// Token: 0x06000CD5 RID: 3285 RVA: 0x0004C5F8 File Offset: 0x0004A7F8
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, short value)
		{
			DeltaDvpTcpNet.<WriteAsync>d__14 <WriteAsync>d__ = new DeltaDvpTcpNet.<WriteAsync>d__14();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<WriteAsync>d__14>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc cref="M:HslCommunication.Core.IReadWriteNet.Write(System.String,System.UInt16)" />
		// Token: 0x06000CD6 RID: 3286 RVA: 0x0004C64C File Offset: 0x0004A84C
		[DebuggerStepThrough]
		public override Task<OperateResult> WriteAsync(string address, ushort value)
		{
			DeltaDvpTcpNet.<WriteAsync>d__15 <WriteAsync>d__ = new DeltaDvpTcpNet.<WriteAsync>d__15();
			<WriteAsync>d__.<>t__builder = AsyncTaskMethodBuilder<OperateResult>.Create();
			<WriteAsync>d__.<>4__this = this;
			<WriteAsync>d__.address = address;
			<WriteAsync>d__.value = value;
			<WriteAsync>d__.<>1__state = -1;
			<WriteAsync>d__.<>t__builder.Start<DeltaDvpTcpNet.<WriteAsync>d__15>(ref <WriteAsync>d__);
			return <WriteAsync>d__.<>t__builder.Task;
		}

		/// <inheritdoc />
		// Token: 0x06000CD7 RID: 3287 RVA: 0x0004C69E File Offset: 0x0004A89E
		public override string ToString()
		{
			return string.Format("DeltaDvpTcpNet[{0}:{1}]", this.IpAddress, this.Port);
		}
	}
}
